package com.trade.common.utils;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * Description：将查询得到的分页结果或者条件查询结果集转换为PageBean对象
 *
 * @Author: Shaok Lei
 * @Date: 2016/3/7 10:56
 * @JDK: 1.8.0_40
 */
public class PageBeanFactory<T> {
    /**
     * 私有化PageBean工厂对象的构造方法
     */
    private PageBeanFactory(){}

    /**
     * 声明PageBean工厂实例
     */
    private static PageBeanFactory instance = null;

    /**
     * 获取PageBean工厂对象，此处设计采用单态的工厂方法
     * @return
     */
    public static synchronized PageBeanFactory getInstance() {
        if (instance==null) {
            instance = new PageBeanFactory();
        }
        return instance;
    }


    /**
     * 将Page对象转换为PageBean对象
     * @param page
     * @return
     */
    public PageBean<T> getPageBeanByPage(Page<T> page,List<T> list){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(page.getNumber());
        pageBean.setPageSize(page.getSize());
        pageBean.setTotalSize(page.getTotalElements());
        pageBean.setTotalPages(page.getTotalPages());
        pageBean.setRows(list);

        return pageBean;
    }

    /**
     * 将Page对象转换为PageBean对象
     * @param page
     * @return
     */
    public PageBean<T> getPageBeanByPage(Page<T> page){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(page.getNumber());
        pageBean.setPageSize(page.getSize());
        pageBean.setTotalSize(page.getTotalElements());
        pageBean.setTotalPages(page.getTotalPages());
        pageBean.setRows(page.getContent());

        return pageBean;
    }

    /**
     * 将Page对象转换为PageBean对象
     * @param rows
     * @return
     */
    public PageBean<T> getPageBeanByList(List<T> rows){
        PageBean<T> pageBean = new PageBean<T>();
        pageBean.setPageNum(-1);
        pageBean.setPageSize(0);
        pageBean.setTotalSize(rows.size());
        pageBean.setTotalPages(0);
        pageBean.setRows(rows);
        return pageBean;
    }

    /***********************************jquery datatables 分页组装类*******************************/

    /**
     * 将Page对象转换为ResponsePaging对象
     *
     * @param page 分页参数
     * @param rows 分页后数据
     * @return ResponsePaging
     */
    public ResponsePaging<T> getResponsePagingByList(Page<T> page, List<T> rows) {
        ResponsePaging<T> responsePaging = new ResponsePaging<>();
        responsePaging.setData(rows);
        responsePaging.setRecordsTotal(page.getTotalElements());
        responsePaging.setRecordsFiltered(page.getTotalElements());
        return responsePaging;
    }
    /**
     * 将Page对象转换为ResponsePaging对象
     *
     * @param page 分页参数
     * @return ResponsePaging
     */
    public ResponsePaging<T> getResponsePagingByList(Page<T> page) {
        ResponsePaging<T> responsePaging = new ResponsePaging<>();
        responsePaging.setData(page.getContent());
        responsePaging.setRecordsTotal(page.getTotalElements());
        responsePaging.setRecordsFiltered(page.getTotalElements());
        return responsePaging;
    }
    /**
     * 将Page对象转换为ResponsePaging对象
     *
     * @param rows 分页后数据
     * @return ResponsePaging
     */
    public ResponsePaging<T> getResponsePagingByList(List<T> rows) {
        ResponsePaging<T> responsePaging = new ResponsePaging<>();
        responsePaging.setData(rows);
        responsePaging.setRecordsTotal(rows.size());
        responsePaging.setRecordsFiltered(rows.size());
        return responsePaging;
    }
}
